package com.osfw.system.service.dict.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.osfw.framework.constants.Constants;
import com.osfw.framework.utils.CacheUtils;
import com.osfw.framework.utils.StringUtils;
import com.osfw.framework.utils.text.Convert;
import com.osfw.system.entity.dict.SysDictData;
import com.osfw.system.mapper.dict.SysDictDataMapper;
import com.osfw.system.service.dict.ISysDictDataService;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 字典 业务层处理
 */
@Service
public class SysDictDataServiceImpl extends ServiceImpl<SysDictDataMapper, SysDictData> implements ISysDictDataService {

    /**
     * 根据条件分页查询字典数据
     *
     * @param dictData 字典数据信息
     * @return 字典数据集合信息
     */
    @Override
    public List<SysDictData> selectDictDataList(SysDictData dictData) {
        LambdaQueryWrapper<SysDictData> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotEmpty(dictData.getDictType()), SysDictData::getDictType, dictData.getDictType());
        queryWrapper.like(StringUtils.isNotEmpty(dictData.getDictLabel()), SysDictData::getDictLabel, dictData.getDictLabel());
        queryWrapper.eq(StringUtils.isNotEmpty(dictData.getStatus()), SysDictData::getStatus, dictData.getStatus());
        queryWrapper.orderByAsc(SysDictData::getDictSort);
        return list(queryWrapper);
    }

    /**
     * 根据字典类型和字典键值查询字典数据信息
     *
     * @param dictType  字典类型
     * @param dictValue 字典键值
     * @return 字典标签
     */
    @Override
    public String selectDictLabel(String dictType, String dictValue) {
        LambdaQueryWrapper<SysDictData> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(SysDictData::getDictLabel);
        queryWrapper.eq(SysDictData::getDictType, dictType);
        queryWrapper.eq(SysDictData::getDictValue, dictValue);
        SysDictData dictData = getOne(queryWrapper);
        return dictData != null ? dictData.getDictLabel() : StringUtils.EMPTY;
    }

    /**
     * 根据字典数据ID查询信息
     *
     * @param dictCode 字典数据ID
     * @return 字典数据
     */
    @Override
    public SysDictData selectDictDataById(Long dictCode) {
        return getById(dictCode);
    }

    /**
     * 批量删除字典数据
     *
     * @param ids 需要删除的数据
     */
    @Override
    public void deleteDictDataByIds(String ids) {
        List<SysDictData> datas = listByIds(Arrays.asList(Convert.toLongArray(ids)));
        for (SysDictData data : datas) {
            removeById(data.getDictCode());
            LambdaQueryWrapper<SysDictData> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SysDictData::getDictType, data.getDictType());
            List<SysDictData> dictDatas = list(queryWrapper);
            CacheUtils.put(getCacheName(), getCacheKey(data.getDictType()), dictDatas);
        }
    }

    /**
     * 新增保存字典数据信息
     *
     * @param data 字典数据信息
     * @return 结果
     */
    @Override
    public boolean insertDictData(SysDictData data) {
        boolean save = save(data);
        if (save) {
            LambdaQueryWrapper<SysDictData> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SysDictData::getDictType, data.getDictType());
            List<SysDictData> dictDatas = list(queryWrapper);
            CacheUtils.put(getCacheName(), getCacheKey(data.getDictType()), dictDatas);
        }
        return save;
    }

    /**
     * 修改保存字典数据信息
     *
     * @param data 字典数据信息
     * @return 结果
     */
    @Override
    public boolean updateDictData(SysDictData data) {
        boolean update = updateById(data);
        if (update) {
            LambdaQueryWrapper<SysDictData> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SysDictData::getDictType, data.getDictType());
            List<SysDictData> dictDatas = list(queryWrapper);
            CacheUtils.put(getCacheName(), getCacheKey(data.getDictType()), dictDatas);
        }
        return update;
    }

    /**
     * 获取cache name
     *
     * @return 缓存名
     */
    public static String getCacheName() {
        return Constants.SYS_DICT_CACHE;
    }

    /**
     * 设置cache key
     *
     * @param configKey 参数键
     * @return 缓存键key
     */
    public static String getCacheKey(String configKey) {
        return Constants.SYS_DICT_KEY + configKey;
    }

}
